/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.repository.depricatedItem;

import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import cz.mzk.oai.metadataRepository.metadata.MetadataFormat;
import cz.mzk.oai.metadataRepository.repository.Identifier;
import cz.mzk.oai.metadataRepository.repository.collections.MetadataCollection;

/**
 *
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class Item {

    private Identifier identifier;
    private String id;
    private MetadataCollection collection;
    private boolean inMarc;
    private boolean inEse;
    private boolean inDc;
    private boolean inTei;
    private static String PROPERTIES_SEPARATOR = " ";
    private DateStamp dateStamp;

    /**
     * @param id identifier
     * @param propertyString String created by Item.toString()
     */
    Item(Identifier id, MetadataCollection collection, String propertyString) {
        this(id, collection, false, false, false, false, null);
        String encodedSeparator = PROPERTIES_SEPARATOR;
        String[] tokens = propertyString.split(encodedSeparator);
        this.inMarc = Boolean.parseBoolean(tokens[0]);
        this.inEse = Boolean.parseBoolean(tokens[1]);
        this.inDc = Boolean.parseBoolean(tokens[2]);
        this.inTei = Boolean.parseBoolean(tokens[3]);
        this.dateStamp = parseDateStamp(tokens[4]);
    }

    private Item(Identifier identifier, MetadataCollection collection, boolean inMarc, boolean inEse, boolean inDc, boolean inTei, DateStamp dateStamp) {
        this.identifier = identifier;
        this.collection = collection;
        this.inMarc = inMarc;
        this.inEse = inEse;
        this.inDc = inDc;
        this.inTei = inTei;
        this.dateStamp = dateStamp;
    }

    /**
     * TODO: zbavit se datestamp u item
     * @param id
     * @param collection
     */
    public Item(Identifier identifier, MetadataCollection collection) {
        this(identifier, collection, false, false, false, false, null);
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @return the collection
     */
    public MetadataCollection getCollection() {
        return collection;
    }

    public boolean hasRecord(MetadataFormat format) {
        switch (format) {
            case ese:
                return inEse;
            case marc21:
                return inMarc;
            case oai_dc:
                return inDc;
            case tei_p5:
                return inTei;
            default:
                return false;
        }
    }

    public void setHasRecord(MetadataFormat format, boolean hasRecord) {
        switch (format) {
            case ese:
                inEse = hasRecord;
                break;
            case marc21:
                inMarc = hasRecord;
                break;
            case oai_dc:
                inDc = hasRecord;
                break;
            case tei_p5:
                inTei = hasRecord;
                break;
        }
    }

    public String toProperty() {
        StringBuilder builder = new StringBuilder();
        builder.append(inMarc);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(inEse);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(inDc);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(inTei);
        builder.append(PROPERTIES_SEPARATOR);
        builder.append(getDateStamp());
        return builder.toString();
    }

    private DateStamp parseDateStamp(String string) {
        //DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
        return new DateStamp(string);
    }

    /**
     * @return the dateStamp
     * @deprecated
     */
    public DateStamp getDateStamp() {
        return dateStamp;
    }

    @Deprecated
    public String getDateStampStr() {
        return dateStamp.toString();
    }

    /**
     * @param dateStamp the dateStamp to set
     * @deprecated
     */
    public void setDateStamp(DateStamp dateStamp) {
        this.dateStamp = dateStamp;
    }

    /**
     * @return the identifier
     */
    public Identifier getIdentifier() {
        return identifier;
    }
}
